package silo.dungeonbuddy;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class Button
{
	private int x;
	private int y;
	private int width;
	private int height;
	private int maxLabelLength;
	private boolean visible;
	//private boolean selected;
	private String label;
	private Color color;
	private Start game;
	private boolean enabled;
	private boolean pressed;
	
	public Button(Start eGame, int eX, int eY, boolean eVis, int eMaxLabelLength, String eLabel, Color eColor,Graphics2D g, boolean eEnabled)
	{
		x=eX;
		y=eY;
		width=g.getFontMetrics().stringWidth(eLabel)+10;
		height=20;
		visible=eVis;
		maxLabelLength=eMaxLabelLength;
		//selected=false;
		label=eLabel;
		color = eColor;
		game=eGame;
		enabled=eEnabled;
		pressed=false;
	}//constructor
	
	public Button(Start eGame, int eX, int eY, boolean eVis, int eMaxLabelLength, String eLabel, Color eColor, boolean eEnabled)
	{
		x=eX;
		y=eY;
		width=50;
		height=20;
		visible=eVis;
		maxLabelLength=eMaxLabelLength;
		//selected=false;
		label=eLabel;
		color = eColor;
		game=eGame;
		enabled=eEnabled;
		pressed=false;
	}//constructor
	
	public void draw(Graphics2D g)
	{
		if(visible)
		{
			if(enabled)
			{
				if(!pressed)
				{
					g.setColor(color);
					g.fillRect(x,y,width,height);
					//Horizontal shade
					g.setColor(Color.darkGray);
					g.drawLine(x,y+height,x+width,y+height);
					//Vertical shade
					g.setColor(Color.darkGray);
					g.drawLine(x+width,y,x+width,y+height);
					//Horizontal highlight
					g.setColor(Color.white);
					g.drawLine(x,y,x+width,y);
					//Vertical highlight
					g.setColor(Color.white);
					g.drawLine(x,y,x,y+height-1);
					//Label
					g.setColor(Color.black);
					g.drawString(label, x+5, (int)Math.floor(y+(height/2))+5);
				}else
				{
					g.setColor(color);
					g.fillRect(x,y,width,height);
					//Horizontal highlight
					g.setColor(Color.white);
					g.drawLine(x,y+height,x+width,y+height);
					//Vertical highlight
					g.setColor(Color.white);
					g.drawLine(x+width,y,x+width,y+height);
					//Horizontal shade
					g.setColor(Color.darkGray);
					g.drawLine(x,y,x+width,y);
					//Vertical shade
					g.setColor(Color.darkGray);
					g.drawLine(x,y,x,y+height-1);
					//Label
					g.setColor(Color.black);
					g.drawString(label, x+5, (int)Math.floor(y+(height/2))+5);
				}
			}else
			{
				g.setColor(Color.gray);
				g.fillRect(x,y,width,height);
				//Horizontal shade
				g.setColor(Color.darkGray);
				g.drawLine(x,y+height,x+width,y+height);
				//Vertical shade
				g.setColor(Color.darkGray);
				g.drawLine(x+width,y,x+width,y+height);
				//Horizontal highlight
				g.setColor(Color.white);
				g.drawLine(x,y,x+width,y);
				//Vertical highlight
				g.setColor(Color.white);
				g.drawLine(x,y,x,y+height-1);
				//Label
				g.setColor(Color.darkGray);
				g.drawString(label, x+5, (int)Math.floor(y+(height/2))+5);
			}
		}
	}//draw
	
	public void action(Graphics2D g)
	{
		// I KNOW THERE A COUPLE OF THINGS WRONG WITH DOING IT THIS WAY
		// I KEEP TELLING MYSELF THAT I'LL CHANGE IT EVENTUALLY, THOUGH
		if(enabled)
		{
			if(label.equalsIgnoreCase("Start server"))
			{
				game.setServer(true);
				game.setClient(false);
				game.destroyForm(g);
			}else if(label.equalsIgnoreCase("Start client"))
			{
				game.setClient(true);
				game.setServer(false);
				game.destroyForm(g);
			}else if(label.equalsIgnoreCase("Launch toolbox"))
			{
				if(game.getToolBox() == null)
				{
					game.setToolBox(new ToolBox("Toolbox", game, 5, 5, 300, 600, "Toolbox"));
					game.getFormList().add(game.getToolBox());
				}else//if(game.getToolBox() == null)
				{
					game.getFormList().add(new Form("frmErrorLaunchToolBox",game,240,240,320,180,"ERROR: The Toolbox is already launched!"));
					Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
					f.getButtons().add(new Button(game,280,155,true,50,"Okay",color,g,true));
					f.getLabels().add(new Label("lblERRORLabel","ERROR: You can only have one toolbox at a time.",10,90,145,20,true));
				}//else -- if(game.getToolBox() == null)
			}else if(label.equalsIgnoreCase("Add piece"))
			{
				enabled=false;
				game.getTextBoxList().add(new TextBox("NewUnitIP",x,y+40,60,20,true,16));
				game.getTextBoxList().add(new TextBox("NewUnitSize",x,y+80,20,20,true,3));
				game.getButtonList().add(new Button(game,x,y+120,true,0,"Confirm addition",color,g,true));
			}else if(label.equalsIgnoreCase("Confirm addition"))
			{
				if(game.getSelectedSquare()!=null)
				{
					if(game.getSelectedSquare().getOccupees().size()<=0)
					{
						String IP="";
						int size = 1;
						int n1=-1;
						int n2=-1;
						for(int i=0;i<game.getTextBoxList().size();i++)
						{
							TextBox t = (TextBox) game.getTextBoxList().get(i);
							if(t.getName().equalsIgnoreCase("NewUnitIP"))
							{
								IP=t.getText();
								n1=i;
							}else if(t.getName().equalsIgnoreCase("NewUnitSize"))
							{
								size=Integer.parseInt(t.getText());
								n2=i;
							}
						}//next i
						game.addUnit(new Unit("Unit : " + game.getUnitList().size(),game,"sprites/default.png",IP,game.getSelectedSquare().getRow(),game.getSelectedSquare().getColumn(),size,size,0));
						if(n1!=-1)
						{
							game.getTextBoxList().remove(n1);
						}
						if(n2!=-1)
						{
							if(n1<n2)
							{
								game.getTextBoxList().remove(n2-1);
							}else
							{
								game.getTextBoxList().remove(n2);
							}
						}
					}
				}
				//game.getTextBoxList().clear();
				for(int i=0; i<game.getButtonList().size();i++)
				{
					Button b = (Button) game.getButtonList().get(i);
					if(b.getLabel().equalsIgnoreCase("Add piece"))
					{
						b.setEnabled(true);
						break;
					}
				}//next i
				game.getButtonList().remove(this);
			}else if(label.equalsIgnoreCase("Move piece"))
			{
				if(game.getSelectedSquare()!=null)
				{
					if(game.getSelectedSquare().getOccupees().size()>0)
					{
						try
						{
							game.setSelectedPiece((Unit)game.getSelectedSquare().getOccupees().get(0));
							enabled=false;
							game.getButtonList().add(new Button(game,x,y+30,true,0,"Confirm move",color,g,true));
						}catch(Exception e)
						{
							game.setSelectedPiece((Terrain)game.getSelectedSquare().getOccupees().get(0));
							enabled=false;
							game.getButtonList().add(new Button(game,x,y+30,true,0,"Confirm move",color,g,true));
						}//catch
					}
				}
			}else if(label.equalsIgnoreCase("Change image"))
			{
				if(game.getSelectedSquare()!=null)
				{
					if(game.getSelectedSquare().getOccupees().size()>0)
					{
						try
						{
							Unit u = (Unit)game.getSelectedSquare().getOccupees().get(0);
							game.setSelectedPiece(u);
						}catch(Exception e)
						{
							Terrain t = (Terrain)game.getSelectedSquare().getOccupees().get(0);
							game.setSelectedPiece(t);
						}
						JFileChooser fc = new JFileChooser();
						File dir = new File("./sprites/");
						fc.setCurrentDirectory(dir);
						fc.showOpenDialog(fc);
						if(fc.getSelectedFile()!=null)
						{
							game.getSelectedPiece().changeImage("sprites/" + fc.getName(fc.getSelectedFile()));
							try
							{
								Unit u2 = (Unit)game.getSelectedPiece();
								u2.setMoved(true);
							}catch(Exception e)
							{
								//nothing for non-units
							}
						}
						game.setSelectedPiece(null);
					}
				}
			}else if(label.equalsIgnoreCase("Confirm move"))
			{
				if(game.getSelectedSquare()!=null)
				{
					try
					{
						Unit u = (Unit)game.getSelectedPiece();
						u.moveTo(game.getSelectedSquare());
						u.setMoved(true);
					}catch(Exception e)
					{
						Terrain t = (Terrain)game.getSelectedPiece();
						t.moveTo(game.getSelectedSquare());
					}
					for(int i=0; i<game.getButtonList().size();i++)
					{
						Button b = (Button) game.getButtonList().get(i);
						if(b.getLabel().equalsIgnoreCase("Move piece"))
						{
							b.setEnabled(true);
							break;
						}
					}//next i
					game.setSelectedPiece(null);
					game.getButtonList().remove(this);
				}
			}else if(label.equalsIgnoreCase("Edit stats"))
			{
				//TODO: I think I'm gonna take out this button.
				if(game.getSelectedSquare()!=null)
				{
					if(game.getSelectedSquare().getOccupees().size()>0)
					{
						try
						{
							game.setSelectedPiece((Unit)game.getSelectedSquare().getOccupees().get(0));
							enabled=false;
							game.getButtonList().add(new Button(game,x,y+145,true,0,"Confirm edits",color,g,true));
							game.getTextBoxList().add(new TextBox("txtPerception",x,y+40,20,20,true,0));
							TextBox t = (TextBox)game.getTextBoxList().get(game.getTextBoxList().size()-1);
							Unit u = (Unit)game.getSelectedPiece();
							t.setText("" + u.getPerception());
							game.getTextBoxList().add(new TextBox("txtStealth",x,y+80,20,20,true,0));
							t = (TextBox)game.getTextBoxList().get(game.getTextBoxList().size()-1);
							t.setText("" + u.getStealth());
							game.getTextBoxList().add(new TextBox("Owner",x,y+120,60,20,true,16));
							t = (TextBox)game.getTextBoxList().get(game.getTextBoxList().size()-1);
							t.setText("" + u.getOwner());
						}catch(Exception e)
						{
							game.setSelectedPiece((Terrain)game.getSelectedSquare().getOccupees().get(0));
						}
					}
				}
			}else if(label.equalsIgnoreCase("Confirm edits"))
			{
				//TODO: This button may be getting removed
				int per = 1;
				int st = 1;
				String owner = "";
				int n1=-1;
				int n2=-1;
				int n3=-1;
				for(int i=0;i<game.getTextBoxList().size();i++)
				{
					TextBox t = (TextBox) game.getTextBoxList().get(i);
					if(t.getName().equalsIgnoreCase("txtPerception"))
					{
						per=Integer.parseInt(t.getText());
						n1=i;
					}else if(t.getName().equalsIgnoreCase("txtStealth"))
					{
						st=Integer.parseInt(t.getText());
						n2=i;
					}else if(t.getName().equalsIgnoreCase("Owner"))
					{
						owner = t.getText();
						n3=i;
					}
				}//next i
				try
				{
					Unit u = (Unit)game.getSelectedPiece();
					u.setPerception(per);
					u.setStealth(st);
					u.setMoved(true);
					u.setOwner(owner);
				}catch(Exception e)
				{
					
				}//catch
				if(n1!=-1)
				{
					game.getTextBoxList().remove(n1);
				}
				if(n2!=-1)
				{
					if(n1<n2)
					{
						game.getTextBoxList().remove(n2-1);
					}else
					{
						game.getTextBoxList().remove(n2);
					}
				}
				if(n3!=-1)
				{
					if((n1<n3)&&(n2<n3))
					{
						game.getTextBoxList().remove(n3-2);
					}else if((n1<n3)||(n2<n3))
					{
						game.getTextBoxList().remove(n3-1);
					}else
					{
						game.getTextBoxList().remove(n3);
					}
				}
				for(int i=0; i<game.getButtonList().size();i++)
				{
					Button b = (Button) game.getButtonList().get(i);
					if(b.getLabel().equalsIgnoreCase("Edit stats"))
					{
						b.setEnabled(true);
						break;
					}
				}//next i
				game.setSelectedPiece(null);
				game.getButtonList().remove(this);
			}else if(label.equalsIgnoreCase("Save board"))
			{
				String name = "";
				for(int i=0;i<game.getTextBoxList().size();i++)
				{
					TextBox tb = (TextBox)game.getTextBoxList().get(i);
					if(tb.getName().equalsIgnoreCase("BoardName"))
					{
						name = tb.getText();
						i=game.getTextBoxList().size();
						break;
					}
				}//next i
				if(name.equals(""))
				{
					game.getFormList().add(new Form("frmErrorSaveBoardNoName",game,240,240,320,180,"ERROR: No file name!"));
					Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
					f.getButtons().add(new Button(game,280,155,true,50,"Okay",color,g,true));
					f.getLabels().add(new Label("lblERRORLabel","ERROR: You have to specify a file name for the board.",10,90,145,20,true));
				}else
				{
					try{
					    // Create file 
					    FileWriter fstream = new FileWriter("./boards/" + name + ".board");
					    BufferedWriter out = new BufferedWriter(fstream);
					    out.write(game.getBoard().toString());
					    //Close the output stream
					    out.close();
					    }catch (Exception e){//Catch exception if any
					      System.err.println("Error: " + e.getMessage());
					    }
				}
			}else if(label.equalsIgnoreCase("Load board"))
			{
				JFileChooser fc = new JFileChooser();
				File dir = new File("./boards/");
				fc.setCurrentDirectory(dir);
				fc.showOpenDialog(fc);
				if(fc.getSelectedFile()!=null)
				{
					if(fc.getSelectedFile().getName().contains(".board"))
					{
						InputMessage IM = new InputMessage(ReadWriteTextFile.getContents(fc.getSelectedFile()),game);
						IM.loadMap();
						String name = fc.getName(fc.getSelectedFile());
						for(int i=0;i<game.getTextBoxList().size();i++)
						{
							TextBox tb = (TextBox)game.getTextBoxList().get(i);
							if(tb.getName().equalsIgnoreCase("BoardName"))
							{
								tb.setText(name);
								i=game.getTextBoxList().size();
								break;
							}
						}//next i
					}
				}
			}else if(label.equalsIgnoreCase("Remove piece"))
			{
				if(game.getSelectedSquare()!=null)
				{
					if(game.getSelectedSquare().getOccupees().size()>0)
					{
						Unit u = (Unit)game.getSelectedSquare().getOccupees().get(0);
						game.removeUnit(u);
					}
				}
			}else if(label.equalsIgnoreCase("Update map"))
			{
				//TODO: Change message to request Map
				game.getTalker().setReturnMessage("!MAP");
			}else if(label.equalsIgnoreCase("New map view"))
			{
				game.getFormList().add(new MapForm("frmMapView" + game.getFormList().size() + System.currentTimeMillis(), game,200,200,800,600,"Map viewer"));
			}else if(label.equalsIgnoreCase("Change back"))
			{
				JFileChooser chooser = new JFileChooser(); // set attributes as appropriate
				JFrame frame = new JFrame();
				BufferedImage image = null;
				try
				{
					image = ImageIO.read(this.getClass().getResource("/sprites/icon.jpg"));
				}catch (IOException e)
				{
					e.printStackTrace();
				}
				frame.setIconImage(image);
				JFileChooser fc = new JFileChooser();
				File dir = new File("./sprites/");
				fc.setMultiSelectionEnabled(false);
				fc.setDialogTitle("Choose an image file for the board's background:");
				//fc.setFileFilter();
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				fc.setCurrentDirectory(dir);
				fc.showOpenDialog(frame);
				if(fc.getSelectedFile()!=null)
				{
					if((fc.getName(fc.getSelectedFile()).contains(".jpg"))||(fc.getName(fc.getSelectedFile()).contains(".png")))
					{
						game.getBoard().setBackground(new Background("Background","sprites/" + fc.getName(fc.getSelectedFile()),game,0,0,50,50));
					}
				}
			}else if(label.equalsIgnoreCase("End turn"))
			{
				game.endTurn();
			}//else if
		}//if(enabled)
	}//action
	
	public void formAction(Form sender, Graphics2D g)
	{
		if(label.equalsIgnoreCase("Confirm edits"))
		{
			StatsForm f = (StatsForm)sender;
			int per = 1;
			int st = 1;
			String owner = "";
			String name = "";
			int maxHp = 10;
			int hp = 10;
			int init = 10;
			int dex = 10;
			int str = 10;
			int con = 10;
			int intel=10;
			int wis = 10;
			int cha = 10;
			int AC  = 10;
			int fort= 10;
			int ref = 10;
			int will= 10;
			int weapon = 0;
			int implement = 0;
			int proficiency = 0;
			for(int i=0;i<f.getTextBoxes().size();i++)
			{
				TextBox t = (TextBox) f.getTextBoxes().get(i);
				if(t.getName().equalsIgnoreCase("Perception"))
				{
					per=Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Stealth"))
				{
					st=Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Owner"))
				{
					owner = t.getText();
				}else if(t.getName().equalsIgnoreCase("Name"))
				{
					name = t.getText();
				}else if(t.getName().equalsIgnoreCase("MaxHp"))
				{
					maxHp = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Hp"))
				{
					hp = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Initiative"))
				{
					init = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Dexterity"))
				{
					dex = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Strength"))
				{
					str = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Constitution"))
				{
					con = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Intelligence"))
				{
					intel = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Wisdom"))
				{
					wis = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Charisma"))
				{
					cha = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("AC"))
				{
					AC = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Fortitude"))
				{
					fort = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Reflex"))
				{
					ref = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Will"))
				{
					will = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Weapon"))
				{
					weapon = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Implement"))
				{
					implement = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Proficiency"))
				{
					proficiency = Integer.parseInt(t.getText());
				}
			}//next i
			if(f.getCheckBoxes().size()>0)
			{
				for(int i=0; i<f.getCheckBoxes().size(); i++)
				{
					CheckBox cK = (CheckBox)f.getCheckBoxes().get(i);
					if(cK.getSelected())
					{
						ClientPlayer cp1 = (ClientPlayer)game.getClientList().get(0);
						for(int j=0; j<game.getClientList().size(); j++)
						{
							ClientPlayer cp = (ClientPlayer)game.getClientList().get(j);
							if(cp.getUserName().equals(cK.getLabel()))
							{
								cp1=cp;
								j=game.getClientList().size();
							}//if(cp.getUserName().equals(cK.getLabel()))
						}//next j
						try
						{
							if(!(f.getUnit().getForceHideList().contains(cp1)))
							{
								f.getUnit().getForceHideList().add(cp1);
							}//if(!(f.getUnit().getForceHideList().contains(cp1)))
						}catch(Exception e)
						{
							
						}//catch e
					}else//if(cK.getSelected())
					{
						ClientPlayer cp1 = (ClientPlayer)game.getClientList().get(0);
						for(int j=0; j<game.getClientList().size(); j++)
						{
							ClientPlayer cp = (ClientPlayer)game.getClientList().get(j);
							if(cp.getUserName().equals(cK.getLabel()))
							{
								cp1=cp;
								j=game.getClientList().size();
							}//if(cp.getUserName().equals(cK.getLabel()))
						}//next j
						try
						{
							if(f.getUnit().getForceHideList().contains(cp1))
							{
								f.getUnit().getForceHideList().remove(cp1);
							}//if(!(f.getUnit().getForceHideList().contains(cp1)))
						}catch(Exception e)
						{
							
						}//catch e
					}//else -- if(cK.getSelected())
				}//next i
			}//if(f.getCheckBoxes().size()>0)
			f.getUnit().setPerception(per);
			f.getUnit().setStealth(st);
			f.getUnit().setMoved(true);
			f.getUnit().setOwner(owner);
			f.getUnit().setName(game.checkUnitName(name,f.getUnit()));
			f.getUnit().setMaxHp(maxHp);
			f.getUnit().setHp(hp);
			f.getUnit().setInitiative(init);
			f.getUnit().setDex(dex);
			f.getUnit().setStrength(str);
			f.getUnit().setConstitution(con);
			f.getUnit().setIntelligence(intel);
			f.getUnit().setWisdom(wis);
			f.getUnit().setCharisma(cha);
			f.getUnit().setAC(AC);
			f.getUnit().setFortitude(fort);
			f.getUnit().setReflex(ref);
			f.getUnit().setWill(will);
			f.getUnit().setMagicWeapon(weapon);
			f.getUnit().setMagicImplement(implement);
			f.getUnit().setWeaponProficiency(proficiency);
			if(game.getServer())
			{
				game.getTurnList().remove(f.getUnit());
				game.addToTurnList(f.getUnit());
			}
			f.close();
		}else if(label.equalsIgnoreCase("Attack this unit"))
		{
			try
			{
				StatsForm s = (StatsForm)sender;
				game.getFormList().add(new AttackForm("frmAttackForm",game,320,240,400,500,"Attack roller",s.getUnit()));
				AttackForm f = (AttackForm)game.getFormList().get(game.getFormList().size()-1);
				f.getLabels().add(new Label("lblAdditions","Check the stats that will be added to this to hit roll:", 5, 20, 380, 20, true));
				f.getCheckBoxes().add(new CheckBox("chkStrength",20,40,true,"Strength"));
				f.getCheckBoxes().add(new CheckBox("chkConstitution",90,40,false,"Constitution"));
				f.getCheckBoxes().add(new CheckBox("chkDexterity",180,40,false,"Dexterity"));
				f.getCheckBoxes().add(new CheckBox("chkIntelligence",250,40,false,"Intelligence"));
				f.getCheckBoxes().add(new CheckBox("chkWisdom",335,40,false,"Wisdom"));
				f.getCheckBoxes().add(new CheckBox("chkCharisma",20,60,false,"Charisma"));
				f.getTextBoxes().add(new TextBox("Other bonuses:",20,90,100,20,true,0));
				f.getLabels().add(new Label("lblVersus","Check the stat that this attack is against:", 5, 140, 380, 20, true));
				f.getCheckBoxes().add(new CheckBox("chkAC",20,160,true,"AC"));
				f.getCheckBoxes().add(new CheckBox("chkFortitude",60,160,false,"Fortitude"));
				f.getCheckBoxes().add(new CheckBox("chkReflex",140,160,false,"Reflex"));
				f.getCheckBoxes().add(new CheckBox("chkWill",200,160,false,"Will"));
				f.getLabels().add(new Label("lblLoadPower","Or load another power :", 5, 260, 380, 20, true));
				f.getButtons().add(new Button(game,5,470,true,0,"Load power",Color.getHSBColor(57, 52, 84),g,true));
				f.getButtons().add(new Button(game,325,470,true,0,"Roll to hit",Color.getHSBColor(57, 52, 84),g,true));
			}catch(Exception e)
			{
				
			}//catch e
		}else if(label.equalsIgnoreCase("Roll to hit"))
		{
			AttackForm a = (AttackForm)sender;
			int roll=Math.abs(game.getGenerator().nextInt()%20)+1;
			int bonuses=0;
			for(int i=0;i<sender.getCheckBoxes().size();i++)
			{
				CheckBox c = (CheckBox)sender.getCheckBoxes().get(i);
				if(c.getName().equals("chkStrength"))
				{
					if(c.getSelected())
					{
						bonuses=bonuses+((game.getMyUnit().getStrength()-10)/2);
					}
				}else if(c.getName().equals("chkConstitution"))
				{
					if(c.getSelected())
					{
						bonuses=bonuses+((game.getMyUnit().getConstitution()-10)/2);
					}
				}else if(c.getName().equals("chkDexterity"))
				{
					if(c.getSelected())
					{
						bonuses=bonuses+((game.getMyUnit().getDex()-10)/2);
					}
				}else if(c.getName().equals("chkIntelligence"))
				{
					if(c.getSelected())
					{
						bonuses=bonuses+((game.getMyUnit().getIntelligence()-10)/2);
					}
				}else if(c.getName().equals("chkWisdom"))
				{
					if(c.getSelected())
					{
						bonuses=bonuses+((game.getMyUnit().getWisdom()-10)/2);
					}
				}else if(c.getName().equals("chkCharisma"))
				{
					if(c.getSelected())
					{
						bonuses=bonuses+((game.getMyUnit().getCharisma()-10)/2);
					}
				}
			}//next i
			try
			{
				TextBox t = (TextBox)sender.getTextBoxes().get(0);
				bonuses=bonuses+Integer.parseInt(t.getText());
			}catch(Exception e)
			{
				sender.getLabels().add(new Label("lblIgnored","You entered a misc. bonus that was ignored because the entry was not a valid integer.", 5, 40, 380, 20, true));
			}//catch e
			a.setRoll(roll);
			a.setBonus(bonuses);
			sender.getLabels().clear();
			sender.getButtons().clear();
			sender.getTextBoxes().clear();
			sender.getLabels().add(new Label("lblRoll","You rolled a " + roll + " with a total bonus of " + bonuses + ".", 5, 20, 380, 20, true));
			for(int i=0;i<sender.getCheckBoxes().size();i++)
			{
				CheckBox c = (CheckBox)sender.getCheckBoxes().get(i);
				if(c.getName().equals("chkAC"))
				{
					if(c.getSelected())
					{
						if(((roll+bonuses)>=a.getUnit().getAC())||(roll==20))
						{
							if(roll==20)
							{
								sender.getLabels().add(new Label("lblHit","Critical hit!", 5, 60, 380, 20, true));
							}else//if(roll==20)
							{
								sender.getLabels().add(new Label("lblHit","You have HIT the enemy!", 5, 60, 380, 20, true));
							}//else -- if(roll==20)
							sender.getTextBoxes().add(new TextBox("Enter damage:",20,250,100,20,true,0));
							sender.getButtons().add(new Button(game,200,470,true,0,"Send damage",Color.getHSBColor(57, 52, 84),g,true));
						}else//if((roll+bonuses)>=a.getUnit().getAC())
						{
							sender.getLabels().add(new Label("lblHit","You have MISSED the enemy", 5, 60, 380, 20, true));
							String mess="";
							String userName = game.getUserName();
							Unit u = null;
							for(int j=0;j<game.getUnitList().size();j++)
							{
								try
								{
									u = (Unit)game.getUnitList().get(j);
									if(u.getOwner().equalsIgnoreCase(userName))
									{
										mess = u.toString();
										j = game.getUnitList().size();
									}//if(u.getOwner().equalsIgnoreCase(userName))
								}catch(Exception e)
								{
									mess="!NO UNIT FROM PLAYER,";
								}//catch
							}//next j
							if(u==null)
							{
								mess="!NO UNIT FROM PLAYER,";
							}//if(u==null)
							game.getTalker().setReturnMessage("!MISS," + a.getUnit().getName() + "," + a.getRoll() + "," + a.getBonus() + "," + userName + "," + mess);
						}//else -- if((roll+bonuses)>=a.getUnit().getAC())
					}//if(c.getSelected())
				}else if(c.getName().equals("chkFortitude"))
				{
					if(c.getSelected())
					{
						if(((roll+bonuses)>=a.getUnit().getFortitude())||(roll==20))
						{
							if(roll==20)
							{
								sender.getLabels().add(new Label("lblHit","Critical hit!", 5, 60, 380, 20, true));
							}else//if(roll==20)
							{
								sender.getLabels().add(new Label("lblHit","You have HIT the enemy!", 5, 60, 380, 20, true));
							}//else -- if(roll==20)
							sender.getTextBoxes().add(new TextBox("Enter damage:",20,250,100,20,true,0));
							sender.getButtons().add(new Button(game,200,470,true,0,"Send damage",Color.getHSBColor(57, 52, 84),g,true));
						}else//if((roll+bonuses)>=a.getUnit().getAC())
						{
							sender.getLabels().add(new Label("lblHit","You have MISSED the enemy", 5, 60, 380, 20, true));
							String mess="";
							String userName = game.getUserName();
							Unit u = null;
							for(int j=0;j<game.getUnitList().size();j++)
							{
								try
								{
									u = (Unit)game.getUnitList().get(j);
									if(u.getOwner().equalsIgnoreCase(userName))
									{
										mess = u.toString();
										j = game.getUnitList().size();
									}//if(u.getOwner().equalsIgnoreCase(userName))
								}catch(Exception e)
								{
									mess="!NO UNIT FROM PLAYER,";
								}//catch
							}//next j
							if(u==null)
							{
								mess="!NO UNIT FROM PLAYER,";
							}//if(u==null)
							game.getTalker().setReturnMessage("!MISS," + a.getUnit().getName() + "," + a.getRoll() + "," + a.getBonus() + "," + userName + "," + mess);
						}//else -- if((roll+bonuses)>=a.getUnit().getAC())
					}
				}else if(c.getName().equals("chkReflex"))
				{
					if(c.getSelected())
					{
						if(((roll+bonuses)>=a.getUnit().getReflex())||(roll==20))
						{
							if(roll==20)
							{
								sender.getLabels().add(new Label("lblHit","Critical hit!", 5, 60, 380, 20, true));
							}else//if(roll==20)
							{
								sender.getLabels().add(new Label("lblHit","You have HIT the enemy!", 5, 60, 380, 20, true));
							}//else -- if(roll==20)
							sender.getTextBoxes().add(new TextBox("Enter damage:",20,250,100,20,true,0));
							sender.getButtons().add(new Button(game,200,470,true,0,"Send damage",Color.getHSBColor(57, 52, 84),g,true));
						}else//if((roll+bonuses)>=a.getUnit().getAC())
						{
							sender.getLabels().add(new Label("lblHit","You have MISSED the enemy", 5, 60, 380, 20, true));
							String mess="";
							String userName = game.getUserName();
							Unit u = null;
							for(int j=0;j<game.getUnitList().size();j++)
							{
								try
								{
									u = (Unit)game.getUnitList().get(j);
									if(u.getOwner().equalsIgnoreCase(userName))
									{
										mess = u.toString();
										j = game.getUnitList().size();
									}//if(u.getOwner().equalsIgnoreCase(userName))
								}catch(Exception e)
								{
									mess="!NO UNIT FROM PLAYER,";
								}//catch
							}//next j
							if(u==null)
							{
								mess="!NO UNIT FROM PLAYER,";
							}//if(u==null)
							game.getTalker().setReturnMessage("!MISS," + a.getUnit().getName() + "," + a.getRoll() + "," + a.getBonus() + "," + userName + "," + mess);
						}//else -- if((roll+bonuses)>=a.getUnit().getAC())
					}
				}else if(c.getName().equals("chkWill"))
				{
					if(c.getSelected())
					{
						if(((roll+bonuses)>=a.getUnit().getWill())||(roll==20))
						{
							if(roll==20)
							{
								sender.getLabels().add(new Label("lblHit","Critical hit!", 5, 60, 380, 20, true));
							}else//if(roll==20)
							{
								sender.getLabels().add(new Label("lblHit","You have HIT the enemy!", 5, 60, 380, 20, true));
							}//else -- if(roll==20)
							sender.getTextBoxes().add(new TextBox("Enter damage:",20,250,100,20,true,0));
							sender.getButtons().add(new Button(game,200,470,true,0,"Send damage",Color.getHSBColor(57, 52, 84),g,true));
						}else//if((roll+bonuses)>=a.getUnit().getAC())
						{
							sender.getLabels().add(new Label("lblHit","You have MISSED the enemy", 5, 60, 380, 20, true));
							String mess="";
							String userName = game.getUserName();
							Unit u = null;
							for(int j=0;j<game.getUnitList().size();j++)
							{
								try
								{
									u = (Unit)game.getUnitList().get(j);
									if(u.getOwner().equalsIgnoreCase(userName))
									{
										mess = u.toString();
										j = game.getUnitList().size();
									}//if(u.getOwner().equalsIgnoreCase(userName))
								}catch(Exception e)
								{
									mess="!NO UNIT FROM PLAYER,";
								}//catch
							}//next j
							if(u==null)
							{
								mess="!NO UNIT FROM PLAYER,";
							}//if(u==null)
							game.getTalker().setReturnMessage("!MISS," + a.getUnit().getName() + "," + a.getRoll() + "," + a.getBonus() + "," + userName + "," + mess);
						}//else -- if((roll+bonuses)>=a.getUnit().getAC())
					}
				}
			}//next i
			sender.getCheckBoxes().clear();
		}else if(label.equalsIgnoreCase("Send damage"))
		{
			try
			{
				AttackForm a = (AttackForm)sender;
				String mess="";
				String userName = game.getUserName();
				Unit u = null;
				for(int j=0;j<game.getUnitList().size();j++)
				{
					try
					{
						u = (Unit)game.getUnitList().get(j);
						if(u.getOwner().equalsIgnoreCase(userName))
						{
							mess = u.toString();
							j = game.getUnitList().size();
						}//if(u.getOwner().equalsIgnoreCase(userName))
					}catch(Exception e)
					{
						mess="!NO UNIT FROM PLAYER,";
					}//catch
				}//next j
				if(u==null)
				{
					mess="!NO UNIT FROM PLAYER,";
				}//if(u==null)
				TextBox t = (TextBox)sender.getTextBoxes().get(0);
				int d = Integer.parseInt(t.getText());
				game.getTalker().setReturnMessage("!HIT," + a.getUnit().getName() + "," + a.getRoll() + "," + a.getBonus() + "," + d + "," + userName + "," + mess);
				sender.close();
			}catch(Exception e)
			{
				game.getFormList().add(new Form("frmErrorInvalidDamage",game,240,240,320,180,"ERROR: Invalid entry!"));
				Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
				f.getButtons().add(new Button(game,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
				f.getLabels().add(new Label("lblERRORLabel","ERROR: The damage that you enter must be a valid integer.",10,90,145,20,true));
			}
		}else if(label.equalsIgnoreCase("Accept"))
		{
			try
			{
				AttackForm a = (AttackForm)sender;
				if(a.getDamage()<=0)
				{
					game.getFormList().add(new Form("frmErrorInvalidDamage",game,240,240,320,180,"ERROR: Invalid damage!"));
					Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
					f.getButtons().add(new Button(game,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
					f.getLabels().add(new Label("lblERRORLabel","ERROR: The specified damage amount is not valid.",10,90,145,20,true));
				}else//if(a.getDamage()==0)
				{
					for(int i=0;i<game.getUnitList().size();i++)
					{
						Unit u = (Unit)game.getUnitList().get(i);
						if(u==a.getUnit())
						{
							u.setHp(u.getHp()-a.getDamage());
						}//if(u==a.getUnit())
					}//next i
				}//else -- if(a.getDamage()==0)
			}catch(Exception e)
			{
				
			}
			sender.close();
		}else if(label.equalsIgnoreCase("Reject"))
		{
			sender.close();
		}else if(label.equalsIgnoreCase("Add piece"))
		{
			//TODO: Add piece button action
			if(game.getSelectedSquare()!=null)
			{
				if(game.getSelectedSquare().getOccupees().size()<=0)
				{
					String Owner="D.M.";
					int size = 1;
					game.addUnit(new Unit("Unit : " + game.getUnitList().size(),game,"sprites/default.png",Owner,game.getSelectedSquare().getRow(),game.getSelectedSquare().getColumn(),size,size,0));
				}
			}
			//game.getTextBoxList().clear();
			SquareForm f = (SquareForm)sender;
			f.close();
		}else if(label.equalsIgnoreCase("Save piece"))
		{
			sender.getTextBoxes().add(new TextBox("Filename",230,340,100,20,true,0));
			sender.getButtons().add(new Button(game,335,340,true,0,"Save",Color.getHSBColor(57, 52, 84),g,true));
			sender.getButtons().add(new Button(game,375,340,true,0,"X",Color.getHSBColor(57, 52, 84),g,true));
			enabled=false;
		}else if(label.equalsIgnoreCase("X"))
		{
			StatsForm s = (StatsForm)sender;
			for(int i=0; i<s.getButtons().size();i++)
			{
				Button b = (Button) s.getButtons().get(i);
				if(b.getLabel().equalsIgnoreCase("Save piece"))
				{
					b.setEnabled(true);
					i=s.getButtons().size();
				}
			}//next i
			for(int i=0; i<s.getButtons().size();i++)
			{
				Button b = (Button) s.getButtons().get(i);
				if(b.getLabel().equalsIgnoreCase("Save"))
				{
					s.getButtons().remove(b);
					i=s.getButtons().size();
				}
			}//next i
			for(int i=0; i<s.getTextBoxes().size();i++)
			{
				TextBox t = (TextBox) s.getTextBoxes().get(i);
				if(t.getName().equalsIgnoreCase("Filename"))
				{
					s.getTextBoxes().remove(t);
					i=s.getTextBoxes().size();
				}
			}//next i
			s.getButtons().remove(this);
		}else if(label.equalsIgnoreCase("Save"))
		{
			StatsForm s = (StatsForm)sender;
			int per = 1;
			int st = 1;
			String owner = "";
			String name = "";
			int maxHp = 10;
			int str = 10;
			int con = 10;
			int intel=10;
			int wis = 10;
			int cha = 10;
			int AC  = 10;
			int fort= 10;
			int ref = 10;
			int will= 10;
			int weapon = 0;
			int implement = 0;
			int proficiency = 0;
			for(int i=0;i<s.getTextBoxes().size();i++)
			{
				TextBox t = (TextBox) s.getTextBoxes().get(i);
				if(t.getName().equalsIgnoreCase("Perception"))
				{
					per=Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Stealth"))
				{
					st=Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Owner"))
				{
					owner = t.getText();
				}else if(t.getName().equalsIgnoreCase("Name"))
				{
					name = t.getText();
				}else if(t.getName().equalsIgnoreCase("MaxHp"))
				{
					maxHp = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Strength"))
				{
					str = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Constitution"))
				{
					con = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Intelligence"))
				{
					intel = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Wisdom"))
				{
					wis = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Charisma"))
				{
					cha = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("AC"))
				{
					AC = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Fortitude"))
				{
					fort = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Reflex"))
				{
					ref = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Will"))
				{
					will = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Weapon"))
				{
					weapon = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Implement"))
				{
					implement = Integer.parseInt(t.getText());
				}else if(t.getName().equalsIgnoreCase("Proficiency"))
				{
					proficiency = Integer.parseInt(t.getText());
				}
			}//next i
			s.getUnit().setPerception(per);
			s.getUnit().setStealth(st);
			s.getUnit().setMoved(true);
			s.getUnit().setOwner(owner);
			s.getUnit().setName(name);
			s.getUnit().setMaxHp(maxHp);
			s.getUnit().setStrength(str);
			s.getUnit().setConstitution(con);
			s.getUnit().setIntelligence(intel);
			s.getUnit().setWisdom(wis);
			s.getUnit().setCharisma(cha);
			s.getUnit().setAC(AC);
			s.getUnit().setFortitude(fort);
			s.getUnit().setReflex(ref);
			s.getUnit().setWill(will);
			s.getUnit().setMagicWeapon(weapon);
			s.getUnit().setMagicImplement(implement);
			s.getUnit().setWeaponProficiency(proficiency);
			if(s.getType()==1)
			{
				//unit
				name = "";
				for(int i=0;i<sender.getTextBoxes().size();i++)
				{
					TextBox tb = (TextBox)sender.getTextBoxes().get(i);
					if(tb.getName().equalsIgnoreCase("Filename"))
					{
						name = tb.getText();
						i=sender.getTextBoxes().size();
					}
				}//next i
				if(name.equals(""))
				{
					System.err.println("No filename was specified");
				}else
				{
					try
					{
					    // Create file 
						FileWriter fstream = new FileWriter("./units/" + name + ".unit");
					    BufferedWriter out = new BufferedWriter(fstream);
					    out.write(s.getUnit().toString());
					    //Close the output stream
					    out.close();
					}catch (Exception e)
					{//Catch exception if any
						System.err.println("Error: " + e.getMessage());
					}
				}
			}else if(s.getType()==2)
			{
				//terrain
				name = "";
				for(int i=0;i<sender.getTextBoxes().size();i++)
				{
					TextBox tb = (TextBox)sender.getTextBoxes().get(i);
					if(tb.getName().equalsIgnoreCase("Filename"))
					{
						name = tb.getText();
						i=sender.getTextBoxes().size();
					}
				}//next i
				if(name.equals(""))
				{
					//TODO: Error handling
					System.err.println("No filename was specified");
				}else
				{
					try
					{
					    // Create file 
						FileWriter fstream = new FileWriter("./terrain/" + name + ".terrain");
					    BufferedWriter out = new BufferedWriter(fstream);
					    if(s.getTerrain().getObstruction())
					    {
					    	out.write(s.getTerrain().getName() + "," + s.getTerrain().getRef() + "," + s.getTerrain().getSizeX() + "," + s.getTerrain().getSizeY() + "," + s.getTerrain().getHeight() + "," + "1" + ",");
					    }else
					    {
					    	out.write(s.getTerrain().getName() + "," + s.getTerrain().getRef() + "," + s.getTerrain().getSizeX() + "," + s.getTerrain().getSizeY() + "," + s.getTerrain().getHeight() + "," + "0" + ",");
					    }
					    //Close the output stream
					    out.close();
					}catch (Exception e)
					{//Catch exception if any
						System.err.println("Error: " + e.getMessage());
					}
				}
			}else if(s.getType()==3)
			{
				//marker
				
			}else if(s.getType()==4)
			{
				//object
				
			}else if(s.getType()==5)
			{
				//effect
				
			}
			s.close();
		}else if(label.equalsIgnoreCase("Okay"))
		{
			sender.close();
		}else if(label.equalsIgnoreCase("Load power"))
		{
			JFileChooser fc = new JFileChooser();
			File dir = new File("./powers/");
			fc.setCurrentDirectory(dir);
			fc.showOpenDialog(fc);
			if(fc.getSelectedFile()!=null)
			{
				if(fc.getSelectedFile().getName().contains(".power"))
				{
					InputMessage IM = new InputMessage(ReadWriteTextFile.getContents(fc.getSelectedFile()),game);
					try
					{
						Power p = IM.readPower();
						AttackForm f = (AttackForm)sender;
						f.getLabels().clear();
						f.getLabels().add(new Label("lblName",p.getName(),5,15,300,20,true));
						f.getCheckBoxes().clear();
						int k=0;
						int num=0;
						String temp="";
						while(k<p.getDescription().length())
						{
							temp=temp+p.getDescription().charAt(k);
							k=k+1;
							if(k<p.getDescription().length())
							{
								if(p.getDescription().charAt(k)=='~')
								{
									f.getLabels().add(new Label("lblDescription" + num,temp,10,30+(15*num),300,20,true));
									temp="";
									num=num+1;
									k=k+1;
								}//if(p.getDamageDescription().charAt(k)=='~')
							}//if(k>=p.getDamageDescription().length())
						}//loop
						f.getLabels().add(new Label("lblDescription",temp,10,30+(15*num),300,20,true));
						f.getTextBoxes().clear();
						f.getLabels().add(new Label("lblPowerType",p.getPowerType() + "  -  ",10,120,300,20,true));
						f.getLabels().add(new Label("lblKeywords",p.getKeywords().replace("|", ", "),10 + g.getFontMetrics().stringWidth(p.getPowerType() + "  -  "),120,300,20,true));
						f.getButtons().clear();
						f.getLabels().add(new Label("lblActionType",p.getActionType(),10,135,300,20,true));
						f.getLabels().add(new Label("lblRange",p.getRange(),10,150,300,20,true));
						f.getLabels().add(new Label("lblTarget",p.getTarget(),10,165,300,20,true));
						f.getLabels().add(new Label("lblAttackRoll","Attack: " + p.getAttribute().replace("|", " or ") + " vs. " + p.getEnemyAttribute().replace("|", " or "),10,190,300,20,true));
						k=0;
						num=0;
						temp="";
						while(k<p.getDamageDescription().length())
						{
							temp=temp+p.getDamageDescription().charAt(k);
							k=k+1;
							if(k<p.getDamageDescription().length())
							{
								if(p.getDamageDescription().charAt(k)=='~')
								{
									f.getLabels().add(new Label("lblDamageDescription" + num,temp,10,215+(15*num),300,20,true));
									temp="";
									num=num+1;
									k=k+1;
								}//if(p.getDamageDescription().charAt(k)=='~')
							}//if(k>=p.getDamageDescription().length())
						}//loop
						f.getLabels().add(new Label("lblDamageDescription" + num,temp,10,215+(15*num),300,20,true));
						f.setPower(p);
						f.getButtons().add(new Button(game,300,470,true,0,"Use this power",Color.getHSBColor(57, 52, 84),g,true));
					}catch(Exception e)
					{
						game.getFormList().add(new Form("frmBadPowerFile",game,240,240,320,180,"ERROR!"));
						Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
						f.getButtons().add(new Button(game,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
						f.getLabels().add(new Label("lblERRORLabel","ERROR: Failed to load requested power.  Try again.",10,90,145,20,true));
						f.getLabels().add(new Label("lblERRORLabel","If the problem persists, the file may be corrupted.",10,105,145,20,true));
					}//catch e
				}//if(fc.getSelectedFile().getName().contains(".power"))
			}//if(fc.getSelectedFile()!=null)
		}else if(label.equalsIgnoreCase("Use this power"))
		{
			AttackForm f = (AttackForm)sender;
			f.getButtons().clear();
			f.getLabels().clear();
			f.getLabels().add(new Label("lblMagicWeapon","If you're using a magic weapon, add the bonus here:",10,45,300,20,true));
			f.getTextBoxes().add(new TextBox("txtMagicWeapon",g.getFontMetrics().stringWidth("If you're using a magic weapon, add the bonus here:")+35,30,50,20,true,0));
			TextBox t = (TextBox)f.getTextBoxes().get(f.getTextBoxes().size()-1);
			t.setText("" + game.getMyUnit().getMagicWeapon());
			f.getLabels().add(new Label("lblMagicImplement","If you're using a magic implement, add the bonus here:",10,75,300,20,true));
			f.getTextBoxes().add(new TextBox("txtMagicImplement",g.getFontMetrics().stringWidth("If you're using a magic implement, add the bonus here:")+20,60,50,20,true,0));
			t = (TextBox)f.getTextBoxes().get(f.getTextBoxes().size()-1);
			t.setText("" + game.getMyUnit().getMagicImplement());
			f.getLabels().add(new Label("lblWeaponProficiency","Add your weapon proficiency bonus here:",10,105,300,20,true));
			f.getTextBoxes().add(new TextBox("txtWeaponProficiency",g.getFontMetrics().stringWidth("Add your weapon proficiency bonus here:")+96,90,50,20,true,0));
			t = (TextBox)f.getTextBoxes().get(f.getTextBoxes().size()-1);
			t.setText("" + game.getMyUnit().getWeaponProficiency());
			f.getLabels().add(new Label("lblOtherBonuses","Add any other bonuses here:",10,135,300,20,true));
			f.getTextBoxes().add(new TextBox("txtOtherBonuses",g.getFontMetrics().stringWidth("Add any other bonuses here:")+163,120,50,20,true,0));
			f.getLabels().add(new Label("lblPenalties","Add any penalties here:",10,165,300,20,true));
			f.getTextBoxes().add(new TextBox("txtPenalties",g.getFontMetrics().stringWidth("Add any penalties here:")+192,150,50,20,true,0));
			f.getLabels().add(new Label("lblCombatAdvantage","Do you have combat advantage?",10,185,300,20,true));
			f.getCheckBoxes().clear();
			f.getCheckBoxes().add(new CheckBox("chkCombatAdvantageYes",10,190,false,"Yes"));
			f.getCheckBoxes().add(new CheckBox("chkCombatAdvantageNo",50,190,false,"No"));
			if(f.getPower().getAttribute().contains("|"))
			{
				f.getLabels().add(new Label("lblWhichAttribute","Which attribute will you use?",10,225,300,20,true));
				int k=0;
				int num=0;
				String temp="";
				while(k<f.getPower().getAttribute().length())
				{
					temp=temp + f.getPower().getAttribute().charAt(k);
					k=k+1;
					if(k<f.getPower().getAttribute().length())
					{
						if(f.getPower().getAttribute().charAt(k)=='|')
						{
							f.getCheckBoxes().add(new CheckBox("chk" + temp,10+(80*num),230,false,temp));
							temp="";
							k=k+1;
							num=num+1;
						}//if(f.getPower().getAttribute().charAt(k)=='|')
					}//if(k<f.getPower().getAttribute().length())
				}//loop
				f.getCheckBoxes().add(new CheckBox("chk" + temp,10+(80*num),230,false,temp));
			}//if(f.getPower().getAttribute().contains("|"))
			f.getButtons().add(new Button(game,f.getWidth()-20-g.getFontMetrics().stringWidth("Attack roll"),470,true,0,"Attack roll",Color.getHSBColor(57, 52, 84),g,true));
		}else if(label.equals("Attack roll"))
		{
			AttackForm a = (AttackForm)sender;
			int roll=Math.abs(game.getGenerator().nextInt()%20)+1;
			int bonus=0;
			int attributeBonus=0;
			for(int i=0;i<sender.getTextBoxes().size();i++)
			{
				TextBox t = (TextBox)sender.getTextBoxes().get(i);
				if(t.getName().equals("txtMagicWeapon"))
				{
					if(a.getPower().getKeywords().contains("Weapon")&&(t.getText().length()>0))
					{
						try
						{
							bonus = bonus + Integer.parseInt(t.getText().replace("+", ""));
						}catch(Exception e)
						{
							game.getFormList().add(new Form("frmErrorInvalidEntry",game,240,240,320,180,"ERROR: Invalid Entry!"));
							Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
							f.getButtons().add(new Button(game,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
							f.getLabels().add(new Label("lblERRORLabel","ERROR: Your entry for magic weapon was not valid; so it was not counted.",10,90,145,20,true));
						}//catch e
					}//if(a.getPower().getKeywords().contains("Weapon")&&(t.getText().length()>0))
				}else if(t.getName().equals("txtMagicImplement"))
				{
					if(a.getPower().getKeywords().contains("Implement")&&(t.getText().length()>0))
					{
						try
						{
							bonus = bonus + Integer.parseInt(t.getText().replace("+", ""));
						}catch(Exception e)
						{
							game.getFormList().add(new Form("frmErrorInvalidEntry",game,240,240,320,180,"ERROR: Invalid Entry!"));
							Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
							f.getButtons().add(new Button(game,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
							f.getLabels().add(new Label("lblERRORLabel","ERROR: Your entry for magic implement was not valid; so it was not counted.",10,90,145,20,true));
						}//catch e
					}//if(a.getPower().getKeywords().contains("Weapon")&&(t.getText().length()>0))
				}else if(t.getName().equals("txtWeaponProficiency"))
				{
					if(a.getPower().getKeywords().contains("Weapon")&&(t.getText().length()>0))
					{
						try
						{
							bonus = bonus + Integer.parseInt(t.getText().replace("+", ""));
						}catch(Exception e)
						{
							game.getFormList().add(new Form("frmErrorInvalidEntry",game,240,240,320,180,"ERROR: Invalid Entry!"));
							Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
							f.getButtons().add(new Button(game,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
							f.getLabels().add(new Label("lblERRORLabel","ERROR: Your entry for weapon proficiency was not valid; so it was not counted.",10,90,145,20,true));
						}//catch e
					}//if(a.getPower().getKeywords().contains("Weapon")&&(t.getText().length()>0))
				}else if(t.getName().equals("txtOtherBonuses"))
				{
					if(t.getText().length()>0)
					{
						try
						{
							bonus = bonus + Integer.parseInt(t.getText().replace("+", ""));
						}catch(Exception e)
						{
							game.getFormList().add(new Form("frmErrorInvalidEntry",game,240,240,320,180,"ERROR: Invalid Entry!"));
							Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
							f.getButtons().add(new Button(game,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
							f.getLabels().add(new Label("lblERRORLabel","ERROR: Your entry for other bonuses was not valid; so it was not counted.",10,90,145,20,true));
						}//catch e
					}//if(a.getPower().getKeywords().contains("Weapon")&&(t.getText().length()>0))
				}else if(t.getName().equals("txtPenalties"))
				{
					if(t.getText().length()>0)
					{
						try
						{
							bonus = bonus - Integer.parseInt(t.getText().replace("-", "").replace("+", ""));
						}catch(Exception e)
						{
							game.getFormList().add(new Form("frmErrorInvalidEntry",game,240,240,320,180,"ERROR: Invalid Entry!"));
							Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
							f.getButtons().add(new Button(game,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
							f.getLabels().add(new Label("lblERRORLabel","ERROR: Your entry for penalties was not valid; so it was not counted.",10,90,145,20,true));
						}//catch e
					}//if(a.getPower().getKeywords().contains("Weapon")&&(t.getText().length()>0))
				}//else if(t.getName().equals("txtPenalties"))
			}//next i
			for(int i=0;i<sender.getCheckBoxes().size();i++)
			{
				CheckBox c = (CheckBox)sender.getCheckBoxes().get(i);
				if(c.getName().equals("chkCombatAdvantageYes"))
				{
					if(c.getSelected())
					{
						bonus=bonus+2;
					}//if(c.getSelected())
				}//if(c.getName().equals("chkCombatAdvantageYes"))
			}//next i
			if(a.getPower().getAttribute().contains("|"))
			{
				for(int i=0;i<sender.getCheckBoxes().size();i++)
				{
					CheckBox c = (CheckBox)sender.getCheckBoxes().get(i);
					if(c.getName().equals("chkStrength"))
					{
						if(c.getSelected())
						{
							bonus=bonus+((game.getMyUnit().getStrength()-10)/2);
							i=sender.getCheckBoxes().size();
						}//if(c.getSelected())
					}else if(c.getName().equals("chkConstitution"))
					{
						if(c.getSelected())
						{
							bonus=bonus+((game.getMyUnit().getConstitution()-10)/2);
							i=sender.getCheckBoxes().size();
						}//if(c.getSelected())
					}else if(c.getName().equals("chkDexterity"))
					{
						if(c.getSelected())
						{
							bonus=bonus+((game.getMyUnit().getDex()-10)/2);
							i=sender.getCheckBoxes().size();
						}//if(c.getSelected())
					}else if(c.getName().equals("chkIntelligence"))
					{
						if(c.getSelected())
						{
							bonus=bonus+((game.getMyUnit().getIntelligence()-10)/2);
							i=sender.getCheckBoxes().size();
						}//if(c.getSelected())
					}else if(c.getName().equals("chkWisdom"))
					{
						if(c.getSelected())
						{
							bonus=bonus+((game.getMyUnit().getWisdom()-10)/2);
							i=sender.getCheckBoxes().size();
						}//if(c.getSelected())
					}else if(c.getName().equals("chkCharisma"))
					{
						if(c.getSelected())
						{
							bonus=bonus+((game.getMyUnit().getCharisma()-10)/2);
							i=sender.getCheckBoxes().size();
						}//if(c.getSelected())
					}//else if(c.getName().equals("chkCharisma"))
				}//next i
			}else if(a.getPower().getAttribute().contains("+"))//if(a.getPower().getAttribute().contains("|"))
			{
				int k = 0;
				String temp = "";
				while(k<a.getPower().getAttribute().length())
				{
					temp = temp + a.getPower().getAttribute().charAt(k);
					k=k+1;
					if(k<a.getPower().getAttribute().length())
					{
						if(a.getPower().getAttribute().charAt(k)=='+')
						{
							if(temp.equals("Strength"))
							{
								bonus=bonus+((game.getMyUnit().getStrength()-10)/2);
							}else if(temp.equals("Constitution"))
							{
								bonus=bonus+((game.getMyUnit().getConstitution()-10)/2);
							}else if(temp.equals("Dexterity"))
							{
								bonus=bonus+((game.getMyUnit().getDex()-10)/2);
							}else if(temp.equals("Intelligence"))
							{
								bonus=bonus+((game.getMyUnit().getIntelligence()-10)/2);
							}else if(temp.equals("Wisdom"))
							{
								bonus=bonus+((game.getMyUnit().getWisdom()-10)/2);
							}else if(temp.equals("Charisma"))
							{
								bonus=bonus+((game.getMyUnit().getCharisma()-10)/2);
							}else
							{
								try
								{
									bonus=bonus+Integer.parseInt(temp);
								}catch(Exception e)
								{
									
								}//catch e
							}//else -- else if(temp.equals("Charisma"))
							temp="";
							k=k+1;
						}//if(a.getPower().getAttribute().charAt(k)=='+')
					}//if(k<a.getPower().getAttribute().length())
				}//loop
				if(temp.equals("Strength"))
				{
					bonus=bonus+((game.getMyUnit().getStrength()-10)/2);
				}else if(temp.equals("Constitution"))
				{
					bonus=bonus+((game.getMyUnit().getConstitution()-10)/2);
				}else if(temp.equals("Dexterity"))
				{
					bonus=bonus+((game.getMyUnit().getDex()-10)/2);
				}else if(temp.equals("Intelligence"))
				{
					bonus=bonus+((game.getMyUnit().getIntelligence()-10)/2);
				}else if(temp.equals("Wisdom"))
				{
					bonus=bonus+((game.getMyUnit().getWisdom()-10)/2);
				}else if(temp.equals("Charisma"))
				{
					bonus=bonus+((game.getMyUnit().getCharisma()-10)/2);
				}else
				{
					try
					{
						bonus=bonus+Integer.parseInt(temp);
					}catch(Exception e)
					{
						
					}//catch e
				}//else -- else if(temp.equals("Charisma"))
			}else//else if(a.getPower().getAttribute().contains("+")) -- if(a.getPower().getAttribute().contains("|"))
			{
				String temp = a.getPower().getAttribute();
				if(temp.equals("Strength"))
				{
					bonus=bonus+((game.getMyUnit().getStrength()-10)/2);
				}else if(temp.equals("Constitution"))
				{
					bonus=bonus+((game.getMyUnit().getConstitution()-10)/2);
				}else if(temp.equals("Dexterity"))
				{
					bonus=bonus+((game.getMyUnit().getDex()-10)/2);
				}else if(temp.equals("Intelligence"))
				{
					bonus=bonus+((game.getMyUnit().getIntelligence()-10)/2);
				}else if(temp.equals("Wisdom"))
				{
					bonus=bonus+((game.getMyUnit().getWisdom()-10)/2);
				}else if(temp.equals("Charisma"))
				{
					bonus=bonus+((game.getMyUnit().getCharisma()-10)/2);
				}//else if(temp.equals("Charisma"))
			}//else -- else if(a.getPower().getAttribute().contains("+")) -- if(a.getPower().getAttribute().contains("|"))
			a.setBonus(bonus);
			a.setRoll(roll);
			a.getLabels().clear();
			a.getTextBoxes().clear();
			a.getButtons().clear();
			a.getCheckBoxes().clear();
			sender.getLabels().add(new Label("lblRoll","You rolled a " + roll + " with a total bonus of " + bonus + ".", 5, 20, 380, 20, true));
			if(a.getPower().getEnemyAttribute().equals("AC"))
			{
				if(((roll+bonus)>=a.getUnit().getAC())||(roll==20))
				{
					if(roll==20)
					{
						sender.getLabels().add(new Label("lblHit","Critical hit!", 5, 60, 380, 20, true));
					}else//if(roll==20)
					{
						sender.getLabels().add(new Label("lblHit","You have HIT the enemy!", 5, 60, 380, 20, true));
					}//else -- if(roll==20)
					int k=0;
					int num=0;
					String temp="";
					Power p = a.getPower();
					while(k<p.getDamageDescription().length())
					{
						temp=temp+p.getDamageDescription().charAt(k);
						k=k+1;
						if(k<p.getDamageDescription().length())
						{
							if(p.getDamageDescription().charAt(k)=='~')
							{
								a.getLabels().add(new Label("lblDamageDescription" + num,temp,10,90+(15*num),300,20,true));
								temp="";
								num=num+1;
								k=k+1;
							}//if(p.getDamageDescription().charAt(k)=='~')
						}//if(k>=p.getDamageDescription().length())
					}//loop
					a.getLabels().add(new Label("lblDamageDescription" + num,temp,10,90+(15*num),300,20,true));
					sender.getTextBoxes().add(new TextBox("Enter damage:",20,280,100,20,true,0));
					sender.getButtons().add(new Button(game,200,470,true,0,"Send damage",Color.getHSBColor(57, 52, 84),g,true));
				}else//if((roll+bonuses)>=a.getUnit().getAC())
				{
					sender.getLabels().add(new Label("lblHit","You have MISSED the enemy", 5, 60, 380, 20, true));
					String mess="";
					String userName = game.getUserName();
					Unit u = null;
					for(int j=0;j<game.getUnitList().size();j++)
					{
						try
						{
							u = (Unit)game.getUnitList().get(j);
							if(u.getOwner().equalsIgnoreCase(userName))
							{
								mess = u.toString();
								j = game.getUnitList().size();
							}//if(u.getOwner().equalsIgnoreCase(userName))
						}catch(Exception e)
						{
							mess="!NO UNIT FROM PLAYER,";
						}//catch
					}//next j
					if(u==null)
					{
						mess="!NO UNIT FROM PLAYER,";
					}//if(u==null)
					game.getTalker().setReturnMessage("!MISS," + a.getUnit().getName() + "," + a.getRoll() + "," + a.getBonus() + "," + userName + "," + mess);
				}//else -- if((roll+bonuses)>=a.getUnit().getAC())
			}else if(a.getPower().getEnemyAttribute().equals("Fortitude"))
			{
				if(((roll+bonus)>=a.getUnit().getFortitude())||(roll==20))
				{
					if(roll==20)
					{
						sender.getLabels().add(new Label("lblHit","Critical hit!", 5, 60, 380, 20, true));
					}else//if(roll==20)
					{
						sender.getLabels().add(new Label("lblHit","You have HIT the enemy!", 5, 60, 380, 20, true));
					}//else -- if(roll==20)
					int k=0;
					int num=0;
					String temp="";
					Power p = a.getPower();
					while(k<p.getDamageDescription().length())
					{
						temp=temp+p.getDamageDescription().charAt(k);
						k=k+1;
						if(k<p.getDamageDescription().length())
						{
							if(p.getDamageDescription().charAt(k)=='~')
							{
								a.getLabels().add(new Label("lblDamageDescription" + num,temp,10,90+(15*num),300,20,true));
								temp="";
								num=num+1;
								k=k+1;
							}//if(p.getDamageDescription().charAt(k)=='~')
						}//if(k>=p.getDamageDescription().length())
					}//loop
					a.getLabels().add(new Label("lblDamageDescription" + num,temp,10,90+(15*num),300,20,true));
					sender.getTextBoxes().add(new TextBox("Enter damage:",20,280,100,20,true,0));
					sender.getButtons().add(new Button(game,200,470,true,0,"Send damage",Color.getHSBColor(57, 52, 84),g,true));
				}else//if((roll+bonuses)>=a.getUnit().getAC())
				{
					sender.getLabels().add(new Label("lblHit","You have MISSED the enemy", 5, 60, 380, 20, true));
					String mess="";
					String userName = game.getUserName();
					Unit u = null;
					for(int j=0;j<game.getUnitList().size();j++)
					{
						try
						{
							u = (Unit)game.getUnitList().get(j);
							if(u.getOwner().equalsIgnoreCase(userName))
							{
								mess = u.toString();
								j = game.getUnitList().size();
							}//if(u.getOwner().equalsIgnoreCase(userName))
						}catch(Exception e)
						{
							mess="!NO UNIT FROM PLAYER,";
						}//catch
					}//next j
					if(u==null)
					{
						mess="!NO UNIT FROM PLAYER,";
					}//if(u==null)
					game.getTalker().setReturnMessage("!MISS," + a.getUnit().getName() + "," + a.getRoll() + "," + a.getBonus() + "," + userName + "," + mess);
				}//else -- if((roll+bonuses)>=a.getUnit().getAC())
			}else if(a.getPower().getEnemyAttribute().equals("Reflex"))
			{
				if(((roll+bonus)>=a.getUnit().getReflex())||(roll==20))
				{
					if(roll==20)
					{
						sender.getLabels().add(new Label("lblHit","Critical hit!", 5, 60, 380, 20, true));
					}else//if(roll==20)
					{
						sender.getLabels().add(new Label("lblHit","You have HIT the enemy!", 5, 60, 380, 20, true));
					}//else -- if(roll==20)
					int k=0;
					int num=0;
					String temp="";
					Power p = a.getPower();
					while(k<p.getDamageDescription().length())
					{
						temp=temp+p.getDamageDescription().charAt(k);
						k=k+1;
						if(k<p.getDamageDescription().length())
						{
							if(p.getDamageDescription().charAt(k)=='~')
							{
								a.getLabels().add(new Label("lblDamageDescription" + num,temp,10,90+(15*num),300,20,true));
								temp="";
								num=num+1;
								k=k+1;
							}//if(p.getDamageDescription().charAt(k)=='~')
						}//if(k>=p.getDamageDescription().length())
					}//loop
					a.getLabels().add(new Label("lblDamageDescription" + num,temp,10,90+(15*num),300,20,true));
					sender.getTextBoxes().add(new TextBox("Enter damage:",20,280,100,20,true,0));
					sender.getButtons().add(new Button(game,200,470,true,0,"Send damage",Color.getHSBColor(57, 52, 84),g,true));
				}else//if((roll+bonuses)>=a.getUnit().getAC())
				{
					sender.getLabels().add(new Label("lblHit","You have MISSED the enemy", 5, 60, 380, 20, true));
					String mess="";
					String userName = game.getUserName();
					Unit u = null;
					for(int j=0;j<game.getUnitList().size();j++)
					{
						try
						{
							u = (Unit)game.getUnitList().get(j);
							if(u.getOwner().equalsIgnoreCase(userName))
							{
								mess = u.toString();
								j = game.getUnitList().size();
							}//if(u.getOwner().equalsIgnoreCase(userName))
						}catch(Exception e)
						{
							mess="!NO UNIT FROM PLAYER,";
						}//catch
					}//next j
					if(u==null)
					{
						mess="!NO UNIT FROM PLAYER,";
					}//if(u==null)
					game.getTalker().setReturnMessage("!MISS," + a.getUnit().getName() + "," + a.getRoll() + "," + a.getBonus() + "," + userName + "," + mess);
				}//else -- if((roll+bonuses)>=a.getUnit().getAC())
			}else if(a.getPower().getEnemyAttribute().equals("Will"))
			{
				if(((roll+bonus)>=a.getUnit().getWill())||(roll==20))
				{
					if(roll==20)
					{
						sender.getLabels().add(new Label("lblHit","Critical hit!", 5, 60, 380, 20, true));
					}else//if(roll==20)
					{
						sender.getLabels().add(new Label("lblHit","You have HIT the enemy!", 5, 60, 380, 20, true));
					}//else -- if(roll==20)
					int k=0;
					int num=0;
					String temp="";
					Power p = a.getPower();
					while(k<p.getDamageDescription().length())
					{
						temp=temp+p.getDamageDescription().charAt(k);
						k=k+1;
						if(k<p.getDamageDescription().length())
						{
							if(p.getDamageDescription().charAt(k)=='~')
							{
								a.getLabels().add(new Label("lblDamageDescription" + num,temp,10,90+(15*num),300,20,true));
								temp="";
								num=num+1;
								k=k+1;
							}//if(p.getDamageDescription().charAt(k)=='~')
						}//if(k>=p.getDamageDescription().length())
					}//loop
					a.getLabels().add(new Label("lblDamageDescription" + num,temp,10,90+(15*num),300,20,true));
					sender.getTextBoxes().add(new TextBox("Enter damage:",20,280,100,20,true,0));
					sender.getButtons().add(new Button(game,200,470,true,0,"Send damage",Color.getHSBColor(57, 52, 84),g,true));
				}else//if((roll+bonuses)>=a.getUnit().getAC())
				{
					sender.getLabels().add(new Label("lblHit","You have MISSED the enemy", 5, 60, 380, 20, true));
					String mess="";
					String userName = game.getUserName();
					Unit u = null;
					for(int j=0;j<game.getUnitList().size();j++)
					{
						try
						{
							u = (Unit)game.getUnitList().get(j);
							if(u.getOwner().equalsIgnoreCase(userName))
							{
								mess = u.toString();
								j = game.getUnitList().size();
							}//if(u.getOwner().equalsIgnoreCase(userName))
						}catch(Exception e)
						{
							mess="!NO UNIT FROM PLAYER,";
						}//catch
					}//next j
					if(u==null)
					{
						mess="!NO UNIT FROM PLAYER,";
					}//if(u==null)
					game.getTalker().setReturnMessage("!MISS," + a.getUnit().getName() + "," + a.getRoll() + "," + a.getBonus() + "," + userName + "," + mess);
				}//else -- if((roll+bonuses)>=a.getUnit().getAC())
			}//else if(a.getPower().getEnemyAttribute().equals("Will"))
		}else if(label.equalsIgnoreCase("Damage this unit"))
		{
			enabled=false;
			sender.getTextBoxes().add(new TextBox("Damage:",x+width+10,370,30,20,true,0));
			sender.getButtons().add(new Button(game,x+width+50,y,true,50,"OK",Color.getHSBColor(57, 52, 84),g,true));
		}else if(label.equals("OK"))
		{
			int damage = 0;
			for(int i=0;i<sender.getButtons().size();i++)
			{
				Button b = (Button)sender.getButtons().get(i);
				if(b.getLabel().equals("Damage this unit"))
				{
					b.setEnabled(true);
					i=sender.getButtons().size();
				}//if(b.getLabel().equals("Damage this unit"))
			}//next i
			for(int i=0;i<sender.getTextBoxes().size();i++)
			{
				TextBox t = (TextBox)sender.getTextBoxes().get(i);
				if(t.getName().equals("Damage:"))
				{
					try
					{
						damage = Integer.parseInt(t.getText());
					}catch (Exception e)
					{
						game.getFormList().add(new Form("frmErrorInvalidDamage",game,240,240,320,180,"ERROR: Invalid damage!"));
						Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
						f.getButtons().add(new Button(game,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
						f.getLabels().add(new Label("lblERRORLabel","ERROR: The specified damage amount is not valid.",10,90,145,20,true));
					}//catch e
					sender.getTextBoxes().remove(t);
					i=sender.getTextBoxes().size();
				}//if(b.getLabel().equals("Damage this unit"))
			}//next i
			sender.getButtons().remove(this);
			for(int i=0;i<sender.getTextBoxes().size();i++)
			{
				TextBox t = (TextBox)sender.getTextBoxes().get(i);
				if(t.getName().equals("Hp"))
				{
					try
					{
						int hp = Integer.parseInt(t.getText())-damage;
						t.setText("" + hp);
						StatsForm f = (StatsForm)sender;
						f.getUnit().setHp(hp);
					}catch (Exception e)
					{
						game.getFormList().add(new Form("frmErrorInvalidDamage",game,240,240,320,180,"ERROR: Invalid damage!"));
						Form f = (Form)game.getFormList().get(game.getFormList().size()-1);
						f.getButtons().add(new Button(game,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
						f.getLabels().add(new Label("lblERRORLabel","ERROR: The specified damage amount is not valid.",10,90,145,20,true));
					}//catch e
					i=sender.getTextBoxes().size();
				}//if(b.getLabel().equals("Damage this unit"))
			}//next i
		}//elseif
	}//formAction
	
	//GETTERS
	public int getX()
	{
		return x;
	}
	public int getY()
	{
		return y;
	}
	public int getWidth()
	{
		return width;
	}
	public int getHeight()
	{
		return height;
	}
	public boolean getVisible()
	{
		return visible;
	}
	//public boolean getSelected()
	//{
		//return selected;
	//}
	public String getLabel()
	{
		return label;
	}
	public Color getColor()
	{
		return color;
	}
	public boolean getEnabled()
	{
		return enabled;
	}
	public boolean getPressed()
	{
		return pressed;
	}
	
	//SETTERS
	public void setX(int eX)
	{
		x=eX;
	}
	public void setY(int eY)
	{
		y=eY;
	}
	public void setWidth(int eWidth)
	{
		width=eWidth;
	}
	public void setHeight(int eHeight)
	{
		height=eHeight;
	}
	public void setVisible(boolean eVis)
	{
		visible = eVis;
	}
	//public void setSelected(boolean eSelected)
	//{
		//selected=eSelected;
	//}
	public void setLabel(String eLabel)
	{
		if((eLabel.length()<maxLabelLength)||(maxLabelLength==0))
		{
			label=eLabel;
			//width=g.getFontMetrics().stringWidth(eLabel)+10;
		}
	}
	public void setColor(Color eColor)
	{
		color = eColor;
	}
	public void setEnabled(boolean eEnabled)
	{
		enabled=eEnabled;
	}
	public void setPressed(boolean ePressed)
	{
		pressed=ePressed;
	}
}//class Button